package cc.z8g.browser.core

import android.app.Activity
import cc.z8g.browser.core.tab.TabViewState
import cc.z8g.browser.database.Bookmark
import cc.z8g.browser.database.HistoryEntry
import cc.z8g.browser.ssl.SslCertificateInfo
import cc.z8g.browser.ssl.showSslDialog
import android.content.Intent
import android.view.View
import cc.z8g.browser.core.view.LongPress

/**
 * 一个在 [BrowserContract.View] 和 [BrowserActivity] 之间的适配器，负责创建部分状态以在活动中呈现。
 */
class BrowserStateAdapter(private val browserActivity: BrowserActivity) : BrowserContract.View {

    private var currentState: BrowserViewState? = null
    private var currentTabs: List<TabViewState>? = null

    override fun renderState(viewState: BrowserViewState) {
        val curr = currentState
        val needUpdateState = PartialBrowserViewState(
            displayUrl = viewState.displayUrl.takeIf { it != curr?.displayUrl },
            sslState = viewState.sslState.takeIf { it != curr?.sslState },
            isRefresh = viewState.isRefresh.takeIf { it != curr?.isRefresh },
            enableFullMenu = viewState.enableFullMenu.takeIf { it != curr?.enableFullMenu },
            themeColor = viewState.themeColor.takeIf { it != curr?.themeColor },
            isForwardEnabled = viewState.isForwardEnabled.takeIf { it != curr?.isForwardEnabled },
            isBackEnabled = viewState.isBackEnabled.takeIf { it != curr?.isBackEnabled },
            bookmarks = viewState.bookmarks.takeIf { it != curr?.bookmarks },
            isBookmarked = viewState.isBookmarked.takeIf { it != curr?.isBookmarked },
            isBookmarkEnabled = viewState.isBookmarkEnabled.takeIf { it != curr?.isBookmarkEnabled },
            isRootFolder = viewState.isRootFolder.takeIf { it != curr?.isRootFolder },
            findInPage = viewState.findInPage.takeIf { it != curr?.findInPage }
        )
        browserActivity.renderState(needUpdateState)

        currentState = viewState
    }

    override fun renderTabs(tabs: List<TabViewState>) {
        tabs.takeIf { it != currentTabs }?.let(browserActivity::renderTabs)
    }

    override fun showAddBookmarkDialog(title: String, url: String, folders: List<String>) {
        browserActivity.showAddBookmarkDialog(title, url, folders)
    }

    override fun showBookmarkOptionsDialog(bookmark: Bookmark.Entry) {
        browserActivity.showBookmarkOptionsDialog(bookmark)
    }

    override fun showEditBookmarkDialog(
        title: String,
        url: String,
        folder: String,
        folders: List<String>
    ) {
        browserActivity.showEditBookmarkDialog(title, url, folder, folders)
    }

    override fun showFolderOptionsDialog(folder: Bookmark.Folder) {
        browserActivity.showFolderOptionsDialog(folder)
    }

    override fun showEditFolderDialog(title: String) {
        browserActivity.showEditFolderDialog(title)
    }

    override fun showHistoryOptionsDialog(historyEntry: HistoryEntry) {
        browserActivity.showHistoryOptionsDialog(historyEntry)
    }

    override fun showFindInPageDialog() {
        browserActivity.showFindInPageDialog()
    }

    override fun showLinkLongPressDialog(longPress: LongPress) {
        browserActivity.showLinkLongPressDialog(longPress)
    }

    override fun showImageLongPressDialog(longPress: LongPress) {
        browserActivity.showImageLongPressDialog(longPress)
    }

    override fun showSslDialog(sslCertificateInfo: SslCertificateInfo) {
        browserActivity.showSslDialog(sslCertificateInfo)
    }

    override fun showCloseBrowserDialog(id: Int) {
        browserActivity.showCloseBrowserDialog(id)
    }

    override fun openBookmarkDrawer() {
        browserActivity.openBookmarkDrawer()
    }

    override fun closeBookmarkDrawer() {
        browserActivity.closeBookmarkDrawer()
    }

    override fun openTabDrawer() {
        browserActivity.openTabDrawer()
    }

    override fun closeTabDrawer() {
        browserActivity.closeTabDrawer()
    }

    override fun showToolbar() {
        browserActivity.showToolbar()
    }

    override fun showLocalFileBlockedDialog() {
        browserActivity.showLocalFileBlockedDialog()
    }

    override fun showFileChooser(intent: Intent) {
        browserActivity.showFileChooser(intent)
    }

    override fun showCustomView(view: View) {
        browserActivity.showCustomView(view)
    }

    override fun hideCustomView() {
        browserActivity.hideCustomView()
    }

    override fun clearSearchFocus() {
        browserActivity.clearSearchFocus()
    }

    override fun getActivity(): Activity {
        return browserActivity
    }
}
